package com.gec.wiki.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.wiki.po.Category;
import com.gec.wiki.mapper.CategoryMapper;
import com.gec.wiki.po.req.CategoryQueryReq;
import com.gec.wiki.po.resp.CategoryQueryResp;
import com.gec.wiki.po.resp.PageResp;
import com.gec.wiki.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gec.wiki.util.CopyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 林云
 * @since 2023-10-30
 */
@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Override
    public PageResp<CategoryQueryReq> getCategoryListByPage(CategoryQueryReq req) {
        Page<Category> page = new Page<>(req.getPage(), req.getSize());
        page = this.page(page);
        List<Category> list = page.getRecords();

        log.info("总行数：{}", page.getTotal() + "");
        log.info("总页数：{}", page.getPages() + "");

        List<CategoryQueryReq> categoryResps = CopyUtil.copyList(list, CategoryQueryReq.class);

        PageResp<CategoryQueryReq> repsPage = new PageResp<>();
        repsPage.setList(categoryResps);
        repsPage.setTotal(page.getTotal());
        return repsPage;
    }

    @Override
    public List<CategoryQueryResp> allList(CategoryQueryReq req) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort");
        List<Category> list = this.list(wrapper);

        List<CategoryQueryResp> resp = CopyUtil.copyList(list, CategoryQueryResp.class);
        return resp;
    }
}
